package org.example.gxpt.pojo.userdetails;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.gxpt.common.constant.RoleConstant;
import org.example.gxpt.pojo.entity.InstrumentAdmin;
import org.example.gxpt.pojo.entity.ShowroomAdmin;
import org.example.gxpt.pojo.entity.SystemAdmin;
import org.example.gxpt.pojo.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginUser implements UserDetails {

    private User user;

    private InstrumentAdmin instrumentAdmin;

    private ShowroomAdmin showroomAdmin;

    private SystemAdmin systemAdmin;

    public LoginUser(User user) {
        this.user = user;
    }

    public LoginUser(InstrumentAdmin instrumentAdmin) {
        this.instrumentAdmin = instrumentAdmin;
    }

    public LoginUser(ShowroomAdmin showroomAdmin) {
        this.showroomAdmin = showroomAdmin;
    }

    public LoginUser(SystemAdmin systemAdmin) {
        this.systemAdmin = systemAdmin;
    }

    private List<String> permissions;

    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        if(authorities != null){
            return authorities;
        }

        if(user != null){
            permissions = new ArrayList<>();
            permissions.add(RoleConstant.USER);
        }else if(instrumentAdmin != null){
            permissions = new ArrayList<>();
            permissions.add(RoleConstant.USER);
            permissions.add(RoleConstant.INSTRUMENT_ADMIN);
        }
        else if(showroomAdmin != null){
            permissions = new ArrayList<>();
            permissions.add(RoleConstant.USER);
            permissions.add(RoleConstant.INSTRUMENT_ADMIN);
            permissions.add(RoleConstant.SHOWROOM_ADMIN);
        }
        else if(systemAdmin != null){
            permissions = new ArrayList<>();
            permissions.add(RoleConstant.USER);
            permissions.add(RoleConstant.INSTRUMENT_ADMIN);
            permissions.add(RoleConstant.SHOWROOM_ADMIN);
            permissions.add(RoleConstant.SYSTEM_ADMIN);
        }

        return permissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        if (user != null) return user.getPassword();
        else if (instrumentAdmin != null) return instrumentAdmin.getPassword();
        else if (showroomAdmin != null) return showroomAdmin.getPassword();
        else return systemAdmin.getPassword();
    }

    @Override
    public String getUsername() {
        if (user != null) return user.getUsername();
        else if (instrumentAdmin != null) return instrumentAdmin.getUsername();
        else if (showroomAdmin != null) return showroomAdmin.getUsername();
        else return systemAdmin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
